package com.boyon.system.action;

import javax.servlet.http.HttpSession;

import org.hibernate.type.LongType;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boyon.common.domain.UserSession;
import com.boyon.common.status.AccountEnmu;
import com.boyon.common.status.AccountType;
import com.boyon.common.status.Constant;
import com.boyon.common.status.StatusType;
import com.boyon.hbm.criteria.OperationType;
import com.boyon.hbm.criteria.Param;
import com.boyon.hbm.criteria.Query;
import com.boyon.system.action.validator.AccountValidator;
import com.boyon.system.entity.Account;
import com.boyon.system.service.AccountService;
import com.boyon.web.param.annotation.ValidatorRequestBody;
import com.boyon.web.response.PageResponse;
import com.boyon.web.response.SuccessResponse;

/**
 * ClassName:AccountController <br/>
 * Function: 账号请求类. <br/>
 * Reason: http外部访问请求url. <br/>
 * Date: 2018年3月13日 下午5:14:38 <br/>
 * 
 * @author sunshulin
 * @version
 * @since JDK 1.8
 * @see
 */
@Controller
@RequestMapping(value = "/boyon/account", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class AccountAction {

	@Autowired
	private AccountService accountService;

	/**
	 * 
	 * createAccount:创建账号请求处理方法 <br/>
	 * 
	 * @author sunshulin
	 * @param account
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/create")
	@ResponseBody
	public SuccessResponse createAccount(
			@ValidatorRequestBody(clazz = AccountValidator.class, method = "create") Account account,
			HttpSession session) {
		UserSession userSession = (UserSession) session
				.getAttribute(Constant.SESSION_USER);
		AccountEnmu accountEnmu = AccountEnmu
				.getType(userSession.getRelationType());
		accountService.createAccount(account, accountEnmu,
				userSession.getRelationId() == null ? 0L
						: userSession.getRelationId());
		return new SuccessResponse();
	}

	/**
	 * 
	 * delete:删除账号. <br/>
	 * 
	 * @author sunshulin
	 * @param account
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public SuccessResponse delete(@ValidatorRequestBody Account account) {
		accountService.deleteAccount(account.getAccountId());
		return new SuccessResponse();
	}

	/**
	 * 
	 * delete:启用/禁用账号. <br/>
	 * 
	 * @author sunshulin
	 * @param account
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/enable")
	@ResponseBody
	public SuccessResponse enable(@ValidatorRequestBody Account account) {
		accountService.enableAccount(account.getAccountId());
		return new SuccessResponse();
	}

	/**
	 * 
	 * resetPassword:重置密码. <br/>
	 * 
	 * @author sunshulin
	 * @param account
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/reset")
	@ResponseBody
	public SuccessResponse reset(@ValidatorRequestBody Account account) {
		accountService.resetPassword(account);
		return new SuccessResponse();
	}

	/**
	 * 
	 * modify:修改密码. <br/>
	 * 
	 * @author sunshulin
	 * @param account
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/modify")
	@ResponseBody
	public SuccessResponse modify(@ValidatorRequestBody Account account) {
		accountService.modifyPassword(account);
		return new SuccessResponse();
	}

	/**
	 * 
	 * query:账号列表查询. <br/>
	 * 
	 * @author sunshulin
	 * @param account
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/query")
	@ResponseBody
	public PageResponse query(@ValidatorRequestBody Account account,
			HttpSession session) {
		Query query = new Query(account.page());
		query.addParam(new Param<String>("account", OperationType.like,
				account.getAccount()));
		query.addParam(new Param<Integer>("accountType", OperationType.eq,
				account.getAccountType()));
		query.addParam(new Param<Integer>("accountType", OperationType.nq,
				AccountType.system.getVal()));
		query.addParam(new Param<Integer>("status", OperationType.eq,
				account.getStatus()));
		query.addParam(new Param<Integer>("status", OperationType.nq,
				StatusType.del.getStatus()));
		UserSession userSession = (UserSession) session
				.getAttribute(Constant.SESSION_USER);
		AccountType accountType = AccountType
				.getType(userSession.getAccountType());
		if (AccountType.system == accountType) {
			query.addParam(new Param<Integer>("accountType", OperationType.eq,
					AccountType.manager.getVal()));
		} else if (AccountType.manager == accountType) {
			query.addParam(new Param<Integer>("accountType", OperationType.eq,
					AccountType.user.getVal()));
		}
		AccountEnmu accountEnmu = AccountEnmu
				.getType(userSession.getRelationType());
		if (AccountEnmu.system == accountEnmu) {
			query.addParam(new Param<Integer>("relationType", OperationType.eq,
					AccountEnmu.system.getVal()));
		} else if (AccountEnmu.agent == accountEnmu) {
			// 代理商账号,查询代理商账号信息
			query.addParam(new Param<Integer>("relationType", OperationType.eq,
					AccountEnmu.agent.getVal()));
			query.addParam(new Param<Object[]>(
					"this_.account_id in(select sys_account_relation.account_id from sys_account_relation where relation_id=?)",
					OperationType.sql,
					new Object[] { userSession.getRelationId() },
					new Type[] { LongType.INSTANCE }));
		}

		return new PageResponse(accountService.query(query), query.getTotal());
	}
}
